package edu.hrbeu.crc.controller;


import edu.hrbeu.crc.constant.ErrorConstant;
import edu.hrbeu.crc.pojo.dto.SavingPlanDTO;
import edu.hrbeu.crc.pojo.query.SavingPlanQuery;
import edu.hrbeu.crc.pojo.result.PageResult;
import edu.hrbeu.crc.pojo.result.Result;
import edu.hrbeu.crc.pojo.vo.SavingPlanPageVO;
import edu.hrbeu.crc.pojo.vo.SavingPlanVO;
import edu.hrbeu.crc.service.SavingPlanService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/savingPlan")
public class SavingPlanController {

    private final SavingPlanService savingPlanService;


    /**
     * 添加计划
     * @param savingPlanDTO
     * @return
     */
    @PostMapping
    public Result add(@RequestBody SavingPlanDTO savingPlanDTO) {
        log.info("添加存钱计划：{}", savingPlanDTO);

        String msg = savingPlanService.add(savingPlanDTO);
        if (msg == null) {
            return Result.success();
        }else {
            return Result.error(msg);
        }
    }

    /**
     * 分页查询
     * @param savingPlanQuery
     * @return
     */
    @GetMapping("/list")
    public Result<PageResult<SavingPlanPageVO>> list(SavingPlanQuery savingPlanQuery) {
        log.info("查看存钱计划：{}", savingPlanQuery);

        PageResult<SavingPlanPageVO> p = savingPlanService.querySavingPlan(savingPlanQuery);
        return Result.success(p);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("根据id查询：{}", id);

        SavingPlanVO vo = savingPlanService.get(id);
        return Result.success(vo);
    }

    /**
     * 修改计划
     * @param savingPlanDTO
     * @return
     */
    @PutMapping
    public Result update(@RequestBody SavingPlanDTO savingPlanDTO) {
        log.info("更新存钱计划：{}", savingPlanDTO);

        savingPlanService.updateSavingPlan(savingPlanDTO);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("批量删除计划：{}",ids);

        savingPlanService.delete(ids);
        return Result.success();
    }
}
